package com.supermap.dataservice.project.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.project.mapper.PmsProductMapper;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.product.PmsProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年10月26 下午 4:35
 * @email 1301584033@qq.com
 */
@Service
public class PmsProductServiceImpl implements PmsProductService {

    protected static final Logger logger = LoggerFactory.getLogger(PmsProductServiceImpl.class);

    @Resource
    private  PmsProductMapper pmsProductMapper;
    @Override
    public JSONUtil getProductByAreaCode(int areaCode,int pageSize,int pageNum) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
            JSONUtil jsonUtil=new JSONUtil();
        List<Map<String,Object>> mapList= pmsProductMapper.
                getPsmProductByAreaCode(areaCode,(pageSize-1)*pageNum,pageNum);

        jsonUtil.setCountSum(pmsProductMapper.getPsmProductByAreaCodeCout(areaCode));

        try {
            return mapList.get(0)!=null?jsonUtil.setData(mapList):
                    jsonUtil.setMsg("查询失败!").setCode(100);
        }catch (Exception e){
            logger.error("{}","根据地区编码产品查询失败!"+e.getMessage());
        }
        return  null;

    }

    @Override
    public Table getAllProductContSum(String beginTime, String endTime) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
        Table tb =new Table();
        try {
            tb.setMsg("操作成功");
            tb.setCode(200);
            tb.setData(pmsProductMapper.getAllProductContSum(beginTime,endTime));
        }catch (Exception e){
            tb.setMsg("操作失败!");
            tb.setCode(100);
        }
        return  tb;
    }

    @Override
    public Table getProductByAreacode(String areaCode,int type) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
        Table tb =new Table();
        tb.setMsg("操作成功");
        tb.setCode(200);
        if(type!=1){
            return  tb;
        }
        try {
                tb.setData(pmsProductMapper.getDsByAreacode(areaCode));
        }catch (Exception e){
            tb.setMsg("操作失败!");
            tb.setCode(100);
        }
        return  tb;

    }

    @Override
    public Table getProductByclassIdAndAreaCode(String bigClassId, String areaCode) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceKey("ds1");
        Table tb =new Table();
        try {
            tb.setMsg("操作成功");
            tb.setCode(200);
            tb.setData(pmsProductMapper.getProductByclassIdAndAreaCode(bigClassId,areaCode));
        }catch (Exception e){
            tb.setMsg("操作失败!");
            tb.setCode(100);
        }
        return  tb;
    }


}
